/**
 * Copyright (c) 2021-2028, iron.guo 郭成杰 (jiedreams@sina.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.beluga.core;

import com.beluga.core.model.*;
import com.beluga.core.model.wechat.*;
import com.beluga.core.notify.WxPayRefundNotifyResult;
import com.beluga.core.pay.BPayChannel;
import com.beluga.core.service.WxPayService;
import com.beluga.core.strategy.BPayStrategy;
import com.beluga.core.util.ContextUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author iron.guo
 * @Date 2022/5/2
 * @Description
 */
@Slf4j
@Service
@AllArgsConstructor
public class IBPay implements BPay{

    private WxPayService wxPayService;


    /************************** 通用接口*********************************/

    /**
     * 通用获取支付令牌
     * @param payModel  {@link PayModel}
     * @return
     */
    @Override
    public Map<String,String> payToken(PayModel payModel) {
        BPayStrategy payStrategy = ContextUtils.getBean(BPayChannel.getImpl(payModel.getChannelId()), BPayStrategy.class);
        return payStrategy.payToken(payModel);
    }

    /*** ################### 微信支付接口 ##############################*/

    /**
     * 微信获取支付令牌
     * @param unifiedOrder  {@link WxBPayUnifiedOrder}
     * @return
     */
    @Override
    public WxPayResult.JsapiResult payToken(WxBPayUnifiedOrder unifiedOrder) {
        return wxPayService.payToken(unifiedOrder);
    }

    /**
     * 微信查询订单
     * @param transactionId 微信订单号
     * @param outTradeNo    商户系统内部的订单号，当没提供transactionId时需要传这个。
     * @return
     */
    @Override
    public WxBPayOrderQuery queryWxOrder(String transactionId, String outTradeNo) {
        return wxPayService.queryWxOrder(transactionId,outTradeNo);
    }

    /**
     * 关闭订单
     * @param outTradeNo 商户系统内部的订单号
     * @return
     */
    @Override
    public WxBPayOrderCloseResult closeWxOrder(String outTradeNo) {
        return wxPayService.closeWxOrder(outTradeNo);
    }

    /**
     * 微信退款申请
     * @param request 请求对象
     * @return
     */
    @Override
    public WxBPayRefundResult wxRefund(WxBPayRefundRequest request) {
        return wxPayService.wxRefund(request);
    }

    @Override
    public WxPayRefundNotifyResult parseWxRefundNotifyResult(String xmlData) {
        return wxPayService.parseWxRefundNotifyResult(xmlData);
    }

    /**
     * 微信退款查询
     */
    @Override
    public WxBPayRefundQueryResult refundQuery(WxBPayRefundQueryRequest request) {
        return wxPayService.refundQuery(request);
    }


}
